{extend name="main"}
{block name="style"}
<style type="text/css">
	.w-60px{width: 60px;}
	.w-80px{width: 80px;}
	.w-150px{width: 150px;}
	.w-350px{width: 350px;}
	.flex-lable{width: 140px;color: #6c757d;text-align: right;padding-right: 10px;}
</style>
{/block}
{block name="body"}
<div class="bg-white p-3">
	<div class="flex pb-3">
		<div class="flex-1">
	        <el-button
	            type="primary"
	            size="mini"
	            @click="window.open('/template/sms/add')">
	            创建模板
	        </el-button>
		</div>
		<div class="small text-secondary">当前共有：{{search.total}}个推送模板</div>
	</div>
	<div class="table-responsive">
		<el-table
	      	:data="tableData"
    		stripe
    		v-loading="search.ajax"
    		style="width: 100%;">
	      <el-table-column
	        prop="id"
	        label="ID"
	        width="60">
	      </el-table-column>
	      <el-table-column
	        prop="alias"
	        label="Alias"
	        width="280">
	      </el-table-column>
		  <el-table-column type="expand" width="20">
			  <template slot-scope="scope">
				{{scope.row.content}}
		      </template>
	      </el-table-column>
	      <el-table-column
	        prop="title"
	        label="名称">
	      </el-table-column>
	      <el-table-column
	        label="状态"
	        width="100"
	        :filter-multiple="false"
		    :filters="[{text: '正常', value: 1}, {text: '停用', value: 0}]"
		    :filter-method="filterHandlerStatus">
		      <template slot-scope="scope">
		      	<transition>
		      		<el-button type="success" size="minism"
		      		v-if="scope.row.status==1 || scope.row.status==2" 
		      		@click="setStatus(scope.row);" 
		      		:loading="scope.row.status==2">正常</el-button>
		      		<el-button type="info" size="minism" 
		      		v-else="scope.row.status==0 || scope.row.status==2" 
		      		@click="setStatus(scope.row);" 
		      		:loading="scope.row.status==2">停用</el-button>
				</transition>
		      </template>
	      </el-table-column>
	      <el-table-column
	        label="/"
	        width="120">
		      <template slot-scope="scope">
		        <el-button
		            type="primary"
		            size="minism"
		            @click="window.open('/template/sms/edit/'+scope.row.id)">
		            编辑
		        </el-button>
		        <el-button
		            type="danger"
		            size="minism"
		            @click="del(scope)">
		            删除
		        </el-button>
		      </template>
	      </el-table-column>
	    </el-table>
	</div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
</script>
<script type="text/javascript">
	var vm=new Vue({
		el:'#app',
		data:{
			view:XYBase.view,
			search:{
				ajax:false,
				total:0
			},
			tableData:[],
		},
		watch:{
		    view:XYBase.viewWatch,
		},
		created(){
			XYBase.created(this);
			this.initHtml();
		},
	    methods: {
	    	conGetHtml:function(str){
		      if (str) {
		        return str.replace(/\n/g, '<br/>');
		      }else{
		        return '';
		      }
		    },
	    	setStatus(data){
	    		if (data.status==2) return;
	    		var status=1,oldStatus=data.status;
	    		if (data.status) status=0;
	    		data.status=2;
	    		$.getJSON('/template/sms/setStatus/'+data.id+'/'+status,function(ret){
	    			data.status=oldStatus;
	    			if (ret) {
	    				if (ret.code === 0) {
	    					data.status=status;
	    				}else{
	    					XYBase.msg(ret.msg);
	    				}
	    			}else{
						XYBase.error(XYBase.config.system_network_error);
	    			}
	    		});
	    	},
			del(rows) {
	      		var _this=this;
		        this.$confirm('是否确定删除【'+rows.row.title+'】模板?', '提示', {
		          confirmButtonText: '删除',
		          cancelButtonText: '取消',
		          type: 'warning'
		        }).then(() => {
		    		const loading=XYBase.openloading();
		    		$.getJSON('/template/sms/delSms/'+rows.row.id,function(ret){
		    			loading.close();
		    			if (ret) {
		    				if (ret.code === 0) {
				      			_this.tableData.splice(rows.$index,1);
		    				}else XYBase.msg(ret.msg);
		    			}else XYBase.error(XYBase.config.system_network_error);
		    		});
		        }).catch(() => {
					XYBase.msg('已取消');
		        });
			},
			resetForm:function(formName) {
				this.$refs[formName].resetFields();
			},
		    filterHandlerStatus(value, row, column) {
        		return row.status === value;
		    },
		    filterHandlerType(value, row, column) {
        		return row.type === value;
		    },
		    filterHandlerIsCompany(value, row, column) {
        		return row.is_company === value;
		    },
	      	initHtml() {
	      		var _this=this;
	      		_this.search.ajax=true;
				$.ajax({
					url:'',
					type:'get',
					data:_this.search,
					dataType:'json',
					error:function(err){
	      				_this.search.ajax=false;
						XYBase.error(XYBase.config.system_network_error);
					},
					success:function(ret){
	      				_this.search.ajax=false;
						if (ret) {
							if (ret.code === 0) {
								_this.search.total=ret.data.total;
								_this.tableData=ret.data.data;
							}else{
								XYBase.msg(ret.msg);
							}
						}else{
							XYBase.error(XYBase.config.system_network_error);
						}
					}
				});
			}
	    }
	});
</script>
{/block}